package com.sjh.lib_app

import android.content.Context
import com.just.agentweb.AgentWebConfig
import com.sjh.baseutils.Preference
import com.sjh.lib_app.bean.UserBean
/**
 *  这是用户的登陆信息记录页面
 * @author : SunJH
 * @data : 2020/8/3
 */
object UserConfig {

    /**
     * 快速保存sp
     */
    fun <T : Any> userPreference(
        name: String,
        defaultValue: T,
        defaultFileName: String = userFileName,
        context: Context = AppApplication.application
    ) = Preference(context, name, defaultValue, defaultFileName)

    private val userFileName = "a09d4469633d45daf637783a304235df"                                          //考试测评的md5值

    /***********************************             user               ***************************/
    var token: String by userPreference("token", "", userFileName)
    var login_first:String by userPreference("login_first", "", userFileName)                 //用户名称
    var password_first:Boolean by userPreference("password_first", false, userFileName)       //用户手机
    var isRememberPassword:Boolean by userPreference("isRememberPassword", false, userFileName) //是否记住密码
    var userAccount: String by userPreference("userAccount", "", userFileName)                  //用户账号
    var userPassword: String by userPreference("userPassword", "", userFileName)                //用户密码

    /**
     * 是否登录
     */
    fun isLogin() = token.isNotEmpty()

    fun clearToken() {
        this.token = ""
    }
    /**
     * 保存用户信息
     */
    fun saveUserData(userBean: UserBean?){
        if(userBean == null) return
        val clazz = userBean::class.java
        val uClazz = UserConfig::class.java
        clazz.declaredFields.forEach {
            try {
                val uField = uClazz.getDeclaredField(it.name+"\$delegate")
                it.isAccessible = true
                val t = it.get(userBean)
                if(t != null){
                    uField.isAccessible = true
                    val t1 = uField.get(this)
                    if(t1 is Preference<*>){
                        t1.saveValue(t)
                    }
                    uField.isAccessible = false
                }
                it.isAccessible = false
            }catch (e:Exception){
//                e.printStackTrace()
            }
        }
    }


    /**
     * 获取本地用户对象
     */
    fun getUserInfo():UserBean{
        if(isLogin()){
            return UserBean(
                token,
                login_first,
                password_first
            )
        }else
            return UserBean()
    }

    /**
     * 清空登录数据
     */
    fun clearData(){
        //清空token相关
        clearUserData()
        AgentWebConfig.removeAllCookies()
    }

    private fun clearUserData() {
        saveUserData(UserBean())
        val uClazz = UserConfig::class.java
        uClazz.declaredFields.forEach {
            if(it.type == Preference::class.java){
                it.isAccessible = true
                val value =it.get(this)
                if(value is Preference<*> && value.default !=null){
                    value.saveValue(value.default!!)
                }
                it.isAccessible = false
            }
        }
    }
}

